﻿using Dapper;
using S_Incident.Domains.EventHandling;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace S_Incident.Infrasturctre
{
    public class DapperRepostitory<T> : IDapperRepository<T> where T : class
    {
        public DapperContext Context;
        public DapperRepostitory(DapperContext Context)
        {
            this.Context = Context;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="insertSql"></param>
        /// <returns></returns>
        public async Task<int> Insert(T entity, string insertSql)
        {
            IDbConnection dbConnection = Context.Command;
            {
                try
                {
                    if (dbConnection.State == ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }
                    return await dbConnection.ExecuteAsync(insertSql, entity);
                }
                finally
                {
                    if (dbConnection.State == ConnectionState.Open)
                    {
                        dbConnection.Close();
                    }
                }
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="deleteSql"></param>
        /// <returns></returns>
        public async Task<int> Delete(int Id, string deleteSql)
        {
            IDbConnection dbConnection = Context.Command;
            {
                try
                {
                    if (dbConnection.State == ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }
                    return await dbConnection.ExecuteAsync(deleteSql, new { Id = Id });
                }
                finally
                {
                    if (dbConnection.State == ConnectionState.Open)
                    {
                        dbConnection.Close();
                    }
                }
            }
        }

        //显示加分页
        public async Task<List<T>> SelectAsync( int PageSize, string selectSql)
        {
            IDbConnection dbConnection = Context.Command;
            {
                try
                {
                    if (dbConnection.State == ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }

                     var list =await Task.Run(() => dbConnection.Query<T>(selectSql,new {PageSize=PageSize}).ToList());

                    return list;
                }
                finally
                {
                    if (dbConnection.State == ConnectionState.Open)
                    {
                        dbConnection.Close();
                    }
                }
            }
        }
        /// <summary>
        /// 查全部
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public async Task<List<T>> GetAllAsync(string sql)
        {
            IDbConnection dbConnection = Context.Command;
            {
                try
                {
                    if (dbConnection.State == ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }

                    return dbConnection.QueryAsync<T>(sql).Result.ToList();
                }
                finally
                {
                    if (dbConnection.State == ConnectionState.Open)
                    {
                        dbConnection.Close();
                    }
                }
            }
        }
        /// <summary>
        /// 查全部事件
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public async Task<List<T>> GetAllAsyncs(string sql)
        {
            IDbConnection dbConnection = Context.Command;
            {
                try
                {
                    if (dbConnection.State == ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }

                    return dbConnection.QueryAsync<T>(sql).Result.ToList();
                }
                finally
                {
                    if (dbConnection.State == ConnectionState.Open)
                    {
                        dbConnection.Close();
                    }
                }
            }
        }




        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="selectOneSql"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<T> GetOne(int Id, string selectOneSql)
        {
            IDbConnection dbConnection = Context.Command;
            {
                try
                {
                    if (dbConnection.State == ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }
                    var result = await dbConnection.QueryFirstOrDefaultAsync<T>(selectOneSql, new { Id = Id });
                    return result;
                }
                finally
                {
                    if (dbConnection.State == ConnectionState.Open)
                    {
                        dbConnection.Close();
                    }
                }
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="updateSql"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Update(T entity, string updateSql)
        {
            IDbConnection dbConnection = Context.Command;
            {
                try
                {
                    if (dbConnection.State == ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }
                    return await dbConnection.ExecuteAsync(updateSql, entity);
                }
                finally
                {
                    if (dbConnection.State == ConnectionState.Open)
                    {
                        dbConnection.Close();
                    }
                }
            }
        }
    }
}
